package com.bing.bingPro.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bing.bingPro.ReflectUtil;
import com.bing.bingPro.controller.dto.TFiveOrderTypeDto;
import com.bing.bingPro.controller.vo.TMoveOrderVo;
import com.bing.bingPro.entity.TMoveOrder;
import com.bing.bingPro.mapper.TMoveOrderMapper;
import com.bing.bingPro.service.TMoveOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Date;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

/**
 * @author qilong
 * @description 针对表【t_move_order】的数据库操作Service实现
 * @createDate
 */
@Service
public class TMoveOrderServiceImpl extends ServiceImpl<TMoveOrderMapper, TMoveOrder>
        implements TMoveOrderService {

    @Override
    public List<TMoveOrderVo> moveList(TFiveOrderTypeDto tOrderYearDto) {
        long start = System.currentTimeMillis();

        Date date = Date.valueOf(tOrderYearDto.getTime());
        List<TMoveOrderVo> collect = baseMapper.selectList(Wrappers.lambdaQuery(TMoveOrder.class)
                        .eq(true, TMoveOrder::getDt, date))
                .stream()
                .map(tMoveOrder -> {
                    TMoveOrderVo vo = new TMoveOrderVo();
                    BeanUtil.copyProperties(tMoveOrder, vo);
                    return vo;
                })
                .collect(Collectors.toList());

        System.out.println("本次导出execl任务执行完毕,共消耗：  " + (System.currentTimeMillis() - start) + "ms");

        return collect;
    }

    @Override
    public List<TMoveOrderVo> exportMove(TFiveOrderTypeDto tOrderYearDto) {
        List<TMoveOrderVo> tMoveOrderVos = this.moveList(tOrderYearDto);
        LocalDate localDate = tOrderYearDto.getTime();
        String dateString = localDate.toString();
        return tMoveOrderVos.parallelStream() // 使用并行流
                .map(tMoveOrder -> {
                    TMoveOrderVo vo = new TMoveOrderVo();
                    try {
                        ReflectUtil.copyAndReplace(tMoveOrder, vo);
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    vo.setDt(dateString);
                    return vo;
                })
                .collect(Collectors.toList());
    }
}




